package com.ctsi.question.service;

import com.ctsi.commons.util.UtilDateTime;
import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.question.dto.*;
import com.ctsi.question.entity.WeekReportEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 周报表 Service 类
 * @author   zhangjw
 * @description  周报表
 * @created	 Mon Aug 26 17:33:18 CST 2019
 */

@Service
public class WeekReportEntityService {
	private static final Logger logger=LoggerFactory.getLogger(WeekReportEntityService.class);
	@Autowired
	private MybatisGenericDao dao;

	/**
	 * 分页获取周报列表
	 * @param reportType
	 * @param userId
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<WeekReportDTO> findReportPage(Integer reportType,String userId, int start , int pageSize){
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1){
			Map<String,Object> param = new HashMap<>();
			param.put("userId",userId);
			param.put("reportType",reportType);
			return dao.queryPage(WeekReportEntity.class.getName()+".queryReportPage", param, start, pageSize);
		} else{
			throw new RuntimeException("参数不正确 !!");
		}
	}


	public WeekReportEntity isExit(String reportTitle,Integer reportType,String submitId){
		Map<String,Object> params = new HashMap<>();
		params.put("reportTitle",reportTitle);
		params.put("reportType",reportType);
		params.put("submitId",submitId);
		WeekReportEntity count = (WeekReportEntity) dao.queryForObject(WeekReportEntity.class.getName()+".reportIsExit",params);
		return count;
	}

	/**
	 * 添加
	 */
	public void insertWeekReportEntity(WeekReportEntity weekReportEntity){
		if(UtilValidate.isEmpty(weekReportEntity.getId())) {
			weekReportEntity.setId(UUIDGenerator.getId());
		}
		logger.debug("insert {}",weekReportEntity);
		dao.insert(weekReportEntity);
	}
	/**
	 * 动态更新
	 */
	public int updateWeekReportEntityByPKSelective(WeekReportEntity weekReportEntity){
		logger.debug("updateByPrimaryKeySelective {}",weekReportEntity);
		return dao.update(WeekReportEntity.class.getName()+".updateByPrimaryKeySelective", weekReportEntity);
	}
	/**
	 * 更新
	 */
	public int updateWeekReportEntity(WeekReportEntity weekReportEntity){
		logger.debug("update {}",weekReportEntity);
		return dao.update(weekReportEntity);
	}


	public WeekReportEntity editWeekReportEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("editById {}",id);
		return (WeekReportEntity)dao.queryForObject(WeekReportEntity.class.getName()+".edit", id);
	}
	/**
	 * 根据ID查找 WeekReportEntity对象
	 */
	public WeekReportEntity findWeekReportEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("findById {}",id);
		return dao.get(WeekReportEntity.class, id);
	}
	/**
	 * 删除WeekReportEntity对象
	 */
	public int removeWeekReportEntity(String id){
		if(UtilValidate.isNotEmpty(id))
			return dao.delete(WeekReportEntity.class.getName()+".delete", id);
			//return dao.remove(id);
		return 0;
	}

	/**
	 * 详细页查询
	 */
	public WeekReportEntity findByDetail(String id){
		return (WeekReportEntity)dao.queryForObject(WeekReportEntity.class.getName()+".findByDetail",id);
	}
	/**
	 * 多条件查询结果
	 */
	public Page<WeekReportEntity> findByMulitCondition(Map<String,Object> param , int start , int pageSize){
		logger.debug("findByMulitCondition {}",param);
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1)
			return dao.queryPage(WeekReportEntity.class.getName()+".selectMulitCondition", param, start, pageSize);
		else{
			throw new RuntimeException("参数不正确 !!");
		}
	}
	/**
	 * 多条件查询结果不分页
	 */
	public List<WeekReportEntity> find(Map<String,Object> map){
		logger.debug("find {}",map);
		return (List<WeekReportEntity>)dao.queryForList(WeekReportEntity.class.getName()+".selectByMap",map);
	}

	/**
	 *
	 * @param leaderId 领导ID
	 * @param nowTime 开始时间
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<SupervisionDTO> findSupervisionDTO(String leaderId,Integer reportType,Long nowTime,int start,int pageSize){
	    if (UtilValidate.isEmpty(leaderId)) {
	        return new Page<>();
        }
	    if (nowTime == null) {
	        nowTime = System.currentTimeMillis();
        }
        /**
         *  开始计算本周的开始时间和结束时间
         */
        long beginTime = UtilDateTime.getDayBeginTimeInWeek(new Date(nowTime),1);
        long endTime = UtilDateTime.getDayEnd(new Date(UtilDateTime.getDayBeginTimeInWeek(new Date(nowTime),5)));
        Map<String,Object> params = new HashMap<>();
        params.put("leaderId",leaderId);
        params.put("reportType",reportType);
        params.put("beginTime",beginTime);
        params.put("endTime",endTime);
        Page<SupervisionDTO> pages = dao.queryPage(WeekReportEntity.class.getName()+".findSupervisionList",params,start,pageSize);
		return pages;
	}

	/**
	 * 人员状态分析
	 * @param leaderId
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<PersonAnalysisDTO> findPersonAnalysis(String leaderId,String personNameLike,int start,int pageSize){
		Map<String,Object> params = new HashMap<>();
		params.put("leaderId",leaderId);
		params.put("personNameLike",personNameLike);
		return dao.queryPage(WeekReportEntity.class.getName()+".personAnalysis",params,start,pageSize);
	}

	/**
	 * 人员项目分析
	 * @param personId
	 * @param projectNameLike
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public Page<PersonProjectDTO> findPersonProjects(String personId,String projectNameLike,int start,int pageSize){
		Map<String,Object> params = new HashMap<>();
		params.put("memberId",personId);
		params.put("projectNameLike",projectNameLike);
		return dao.queryPage(WeekReportEntity.class.getName()+".findpersonProjects",params,start,pageSize);
	}

	/**
	 * 通过项目ID，查询项目详情
	 * @param projectId
	 * @return
	 */
	public ProjectPlanInfo findProjectInfo(String projectId){
		if (UtilValidate.isEmpty(projectId)) {
			return new ProjectPlanInfo();
		}
		return (ProjectPlanInfo) dao.queryForObject(WeekReportEntity.class.getName()+".findProjectPlanInfo",projectId);
	}

	/**
	 * 根据用户ID，查询所属的项目列表
	 * @param userId
	 * @return
	 */
	public List<Map<String,Object>> findProjectsByUserId(String userId){
		if (UtilValidate.isEmpty(userId)) {
			return Collections.emptyList();
		}
		Map<String,Object> params = new HashMap<>();
		params.put("curUserId",userId);
		return (List<Map<String, Object>>) dao.queryForList(WeekReportEntity.class.getName()+".findUserProjects",params);
	}
}
